package com.iuie.ns.system.controller.role;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.iuie.ns.frame.exception.ServiceException;
import com.iuie.ns.frame.utils.StringUtils;
import com.iuie.ns.frame.web.dataset.vo.Page;
import com.iuie.ns.frame.web.response.vo.ResponseInfo;
import com.iuie.ns.system.service.role.ISysRoleService;
import com.iuie.ns.system.vo.resource.SysResourceVO;
import com.iuie.ns.system.vo.role.SysRoleVO;

@Controller
@RequestMapping("sys/role")
public class SysRoleController {

	@Resource
	private ISysRoleService roleService;
	
	private static final Logger logger = LoggerFactory.getLogger(SysRoleController.class);
	
	@RequestMapping
	public String index(){
		return "view-system/role/role";
	}
	
	@RequestMapping(value="index/add")
	public String addRoleIndex() {
		return "view-system/role/roleAdd";
	}
	
	@RequestMapping(value="index/update/{roleId}")
	public String updateRoleIndex(@PathVariable("roleId") String roleId, HttpServletRequest request) {
		request.setAttribute("roleId", roleId);
		return "view-system/role/roleUpdate";
	}
	
	@RequestMapping(value = "findAllByPage", method=RequestMethod.GET)
	@ResponseBody
	public ResponseInfo findAllByPage(SysRoleVO role, Page page) {
		List<SysRoleVO> list = roleService.findAllByPage(role, page);
		ResponseInfo ri = new ResponseInfo(page, list);
		return ri;
	}
	
	/**
	 * 根据角色id查找角色的菜单
	 * @param roleId
	 * @return
	 * @date 2017年7月21日 上午8:48:44
	 */
	@RequestMapping(value = "findMenusById/{roleId}", method=RequestMethod.GET)
	@ResponseBody
	public ResponseInfo findMenusById(@PathVariable("roleId") String roleId) {
		List<SysResourceVO> list = roleService.findMenusById(roleId);
		ResponseInfo ri = new ResponseInfo(list);
		return ri;
	}
	
	
	@RequestMapping(value="saveRelation/{roleId}", method=RequestMethod.POST) 
	@ResponseBody
	public ResponseInfo saveRelation(@PathVariable("roleId") String roleId, 
	        @RequestParam(required=false,value="menuIds[]") String[] menuIds) {
	    roleService.saveRelation(roleId, menuIds);
	    ResponseInfo ri = new ResponseInfo();
	    return ri;
	}
	
	@RequestMapping(value="add",method=RequestMethod.POST)
	@ResponseBody
	public ResponseInfo addRole(SysRoleVO role) {
	    if (StringUtils.isEmpty(role.getRoleId())) {
	        role.setRoleId(StringUtils.uuid());
	    }
	    try {
	    	roleService.addOne(role);
		} catch (DataIntegrityViolationException e) {
			throw new ServiceException("创建角色失败，请确保角色名称和角色编码全表唯一");
		}
	    return new ResponseInfo().setMessage("新增成功！");
	}
	
	@RequestMapping(value="delete/{roleId}",method=RequestMethod.POST)
	@ResponseBody
	public ResponseInfo deleteRole(@PathVariable("roleId") String roleId) {
		if (StringUtils.isEmpty(roleId)) {
			throw new ServiceException("删除角色失败，原因：roleId为空");
		}
		logger.info("正在根据角色ID删除角色，要删除的角色的ID为： " + roleId);
		roleService.deleteById(roleId);
		return new ResponseInfo().setMessage("删除成功！");
	}
	
	@RequestMapping(value="update/{roleId}",method=RequestMethod.POST)
	@ResponseBody
	public ResponseInfo updateRole(@PathVariable("roleId") String roleId, SysRoleVO role) {
		if (StringUtils.isEmpty(roleId)) {
			throw new ServiceException("更新角色失败，原因：roleId为空");
		}
		logger.info("正在更新角色，角色的ID为： " + roleId);
		try {
			roleService.updateRole(role);
		} catch (DuplicateKeyException e) {
			throw new ServiceException("更新角色失败，请确保角色名称和角色编码全表唯一");
		}
		return new ResponseInfo().setMessage("更新成功！");
	}
	/**
	 * 根据角色ID查询角色信息
	 * @param roleId
	 * @return
	 * @date 2017年7月25日 上午12:45:24
	 */
	@RequestMapping(value="findOne/{roleId}")
	@ResponseBody
	public ResponseInfo findOne(@PathVariable("roleId") String roleId) {
		if (StringUtils.isEmpty(roleId)) {
			throw new ServiceException("查询角色失败，原因：roleId为空");
		}
		logger.info("正在查询角色，角色的ID为： " + roleId);
		SysRoleVO role = roleService.findById(roleId);
		return new ResponseInfo(role).setMessage("更新成功！");
	}
}
